#include "demo.h"
#include "codeports.h"

typedef struct
{
	BOX box;
	float s;
} Plasma;

void
DEMO_Init(DEMO *demo, BOX *box)
{
	Plasma *plasma;
	demo->box = box;
	demo->data = malloc(sizeof(Plasma));
	plasma = demo->data;
	BOX_Allocate(&plasma->box, box->info.width, box->info.height);
	plasma->s = (rand() % 5 + 1) * 0.5f;
}

#define dist(a, b, c, d) sqrtf((a - c) * (a - c) + (b - d) * (b - d))

void
DEMO_Frame(DEMO *demo)
{
	Plasma *plasma = demo->data;
	float x, y, c;
	plasma->s += (rand() % 5 + 1) * 0.5f;
	for(x = 0; x < plasma->box.info.width; x += 1.0f)
	{
		for(y = 0; y < plasma->box.info.height; y += 1.0f)
		{
			c = (4.0f + (sinf(dist(x + plasma->s, y, 128.0f, 128.0f) / 8.0f) + sinf(dist(x, y, 64.0f, 64.0f) / 8.0f) + sinf(dist(x, y + plasma->s / 8.0f, 192.0f, 64) / 8.0f) + sinf(dist(x, y, 192.0f, 100.0f) / 8.0f))) * 32.0f;
			BOX_SetPixelRGB(&plasma->box, x, y, c / 2, c * 2, 255 - c);
			BOX_SetPixelRGB(demo->box, x, y, c / 2, c * 2, 255 - c);
		}
	}
}

void
DEMO_Final(DEMO *demo)
{
	Plasma *plasma = demo->data;
	BOX_Free(&plasma->box);
	free(demo->data);
}
